package com.itheima.service;

import com.itheima.dto.AnimalDTO;
import com.itheima.entity.Animal;
import com.itheima.repository.AnimalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AnimalService {

    @Autowired
    private AnimalRepository animalRepository;

    public Animal addAnimal(Animal animal) {
        animal.setCreatedAt(new Date());
        return animalRepository.save(animal);
    }

    // 获取所有动物信息
    public List<Animal> getAllAnimals() {
        return animalRepository.findAll();
    }

    // 获取已审核的动物信息
    public List<Animal> getApprovedAnimals() {
        return animalRepository.findAnimalsByReviewStatusNative("APPROVED");
    }

    public Animal updateAnimal(Long id, AnimalDTO animalDTO) {
        Animal animal= animalRepository.findById(id).get();
        if (animalDTO.getSpecies() != null) {
            animal.setSpecies(animalDTO.getSpecies());
        }
        if (animalDTO.getSize() != null) {
            animal.setSize(animalDTO.getSize());
        }
        if (animalDTO.getColor() != null) {
            animal.setColor(animalDTO.getColor());
        }
        if (animalDTO.getLocation() != null) {
            animal.setLocation(animalDTO.getLocation());
        }
        if (animalDTO.getInjured() != null) {
            animal.setInjured(animalDTO.getInjured());
        }
        if (animalDTO.getDescription() != null) {
            animal.setDescription(animalDTO.getDescription());
        }
        if (animalDTO.getPhotoUrl() != null) {
            animal.setPhotoUrl(animalDTO.getPhotoUrl());
        }
        if (animalDTO.getUsername() != null) {
            animal.setUsername(animalDTO.getUsername());
        }
        animal.setUpdatedAt(new Date());


        return animalRepository.save(animal);
    }

    public void deleteAnimal(Long id) {
        animalRepository.deleteById(id);
    }


    public List<Animal> searchAnimals(String species, String size, String color, String location) {
        return animalRepository.searchByCriteria(species, size, color, location);
    }
    public List<Animal> searchApproveAnimals(String species, String size, String color, String location) {
        return animalRepository.searchCriteriaByReviewStatusNative(species, size, color, location,"APPROVED");
    }
}
